import * as apis from '../services/examinationService'

export  default {
    namespace:'mechanismPage',

    state:{
        mechanismList:[],
        mechanismVList:[],
        mechanismTList:[],
        mechanismVList4:[],
        mechanismTList4:[],

    },
    reducers:{
       setMechanismList:setMechanismList,
       setVerificationmechanismList:setVerificationmechanismList,
       setTestmechanismList:setTestmechanismList
    },

    effects:{
        getMechanismList: getMechanismList,
        getVerificationmechanismList:getVerificationmechanismList,
        getTestmechanismList:getTestmechanismList
    }

}

function setMechanismList(state,payload){
    let _state= JSON.parse(JSON.stringify(state));
    _state.mechanismList=payload.data.data
    return _state
}


function setVerificationmechanismList(state,payload){
    let _state= JSON.parse(JSON.stringify(state));
    _state.mechanismVList=payload.data.data
    _state.mechanismVList.sort(compare("verificationCount"))
    if(_state.mechanismVList.length<4){
     _state.mechanismVList4= _state.mechanismVList
    }
    else{
        _state.mechanismVList4=[_state.mechanismVList[0],_state.mechanismVList[1],_state.mechanismVList[2],_state.mechanismVList[3]]
    }
    
    return _state
}

function setTestmechanismList(state,payload){
    let _state= JSON.parse(JSON.stringify(state));
    _state.mechanismTList=payload.data.data
    _state.mechanismTList.sort(compare("verificationCount"))
    if(_state.mechanismTList.length<4){
        _state.mechanismTList4= _state.mechanismTList
       }
       else{
           _state.mechanismTList4=[_state.mechanismTList[0],_state.mechanismTList[1],_state.mechanismTList[2],_state.mechanismTList[3]]
       }
    return _state
}

function* getMechanismList(payload,{select,call,put}){
    let result = yield call(apis.getMechanismList)
    yield put({
        type:"setMechanismList",
        data:result.data
    })
    
}

function* getVerificationmechanismList(payload,{select,call,put}){
    let result = yield call(apis.getVerificationmechanismList)
    yield put({
        type:"setVerificationmechanismList",
        data:result.data
    })
}

function* getTestmechanismList(payload,{select,call,put}){
    let result = yield call(apis.getTestmechanismList)
    yield put({
        type:"setTestmechanismList",
        data:result.data
    })
}

function compare(verificationCount){
    return function(a,b){
        return b[verificationCount]-a[verificationCount]
    }
}
